/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rultax.blackice.physics.panel;

import com.rultax.blackice.physics.Physics;
import java.awt.geom.Point2D;
import javax.swing.JPanel;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;

/**
 *
 * @author scottw
 */
public class PanelPhysics extends Physics<Vec2, Point2D.Float> {
    private World world;
    private JPanel screen;
    
    private float transX;
    private float transY;
    private float scaleFactor;
    private float yFlip;
    
    public PanelPhysics(JPanel screen){
	this(screen, 10f, screen.getWidth() / 2, screen.getHeight() / 2);
    }
    
    
    public PanelPhysics(JPanel screen, float scaleFactor, float transX, float transY){
	super(new PanelCoordConverter(screen, transX, transY, scaleFactor),
		new PanelVectorConverter(screen, transX, transY, scaleFactor));
	this.screen = screen;
	this.scaleFactor = scaleFactor;
	this.transX = transX;
	this.transY = transY;
	yFlip = -1;
    }

    @Override
    public void step() {
	float timeStep = 1.0f / 60f;
	this.step(timeStep, 10, 10);
    }

    private void step(float timeStep, int velocityIterations, int positionIterations){
	world.step(timeStep, velocityIterations, positionIterations);
    }
    
    @Override
    public void createWorld() {
	Vec2 gravity = new Vec2(0, -9.8f);
	createWorld(gravity, true);
    }
    
    private void createWorld(Vec2 gravity, boolean doSleep){
	world = new World(gravity, doSleep);
    }

    @Override
    public void setGravity(float x, float y) {
	world.setGravity(new Vec2(x, y));
    }

    @Override
    public float scalarScreenToWorld(float val) {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public float scalarWorldToScreen(float val) {
	throw new UnsupportedOperationException("Not supported yet.");
    }
}
